//
//  SNKVideoNavController.m
//  SleepNews
//
//  Created by Kavee DJ on 2016/12/19.
//  Copyright © 2016年 Kavee DJ. All rights reserved.
//

#import "SNKVideoNavController.h"
#import "SNKVideoViewController.h"
#import "SNKNavTabBar.h"
#import "SNKWeatherViewController.h"
#import <DKNightVersion.h>

@interface SNKVideoNavController () <UIScrollViewDelegate,SNKNavTabBarDelegate>
{
    NSInteger            _currentIndex;
    NSMutableArray       *_titles;
    SNKNavTabBar         *_navTabBar;
    UIScrollView         *_mainView;
}

@end

@implementation SNKVideoNavController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    [self setupControllers];
    
    [self setupConfigure];
    
    [self setupUI];
}

- (void)setupControllers {
    
    NSArray *urlArray = @[@"VAP4BFE3U",
                          @"VAP4BFR16",
                          @"VAP4BG6DL",
                          @"V9LG4CHOR",
                          @"VAP4BGTVD",
                          @"V9LG4E6VR",
                          @"00850FRB"];
    
    NSArray *nameArray = [NSArray array];
    nameArray = @[@"奇葩",@"萌宠",@"美女",@"娱乐",@"精品",@"搞笑",@"趣味"];
    
    NSMutableArray *viewArray = [NSMutableArray array];
    
    for (NSInteger i = 0; i < nameArray.count; i++) {
        SNKVideoViewController *otherViewController = [[SNKVideoViewController alloc] init];
        otherViewController.title = nameArray[i];
        otherViewController.url = urlArray[i];
        [viewArray addObject:otherViewController];
    }
    
    _subViewControllers = [NSArray array];
    _subViewControllers = viewArray;
}

- (void)setupConfigure {
    _currentIndex = 1;
    
    _titles = [[NSMutableArray alloc] initWithCapacity:_subViewControllers.count];
    
    for (UIViewController *viewController in _subViewControllers) {
        
        [_titles addObject:viewController.title];
    }
}

- (void)setupUI {
    [self setupView];
    
    // 首先加载第一个视图
    UIViewController *viewController = (UIViewController *)_subViewControllers[0];
    viewController.view.frame = CGRectMake(0, 0, SCREENW, SCREENH);
    [_mainView addSubview:viewController.view];
    [self addChildViewController:viewController];
}

- (void)setupView {
    // 加载导航条
    _navTabBar = [[SNKNavTabBar alloc] initWithFrame:CGRectMake(0, 0, SCREENW, 64)];
    _navTabBar.dk_backgroundColorPicker = DKColorPickerWithKey(BG);
    _navTabBar.delegate = self;
    _navTabBar.lineColor = _navTabBarLineColor;
    _navTabBar.itemTitles = _titles;
    [_navTabBar updateData];
    [self.view addSubview:_navTabBar];
    
    // 加载ScrollView
    _mainView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, SCREENW, SCREENH - 64)];
    _mainView.delegate = self;
    _mainView.pagingEnabled = YES;
    _mainView.bounces = _canMainViewBounces;
    _mainView.showsHorizontalScrollIndicator = NO;
    _mainView.showsVerticalScrollIndicator = NO;
    _mainView.contentSize = CGSizeMake(SCREENW * _subViewControllers.count, 0);
    [self.view addSubview:_mainView];
    
    // 导航条下分割线,就是这条白线
//    UIView *lineV = [[UIView alloc] initWithFrame:CGRectMake(0, 64, SCREENW, 1)];
//    lineV.backgroundColor = [UIColor colorWithRed:216/255.0f green:216/255.0f blue:216/255.0f alpha:1];
//    [self.view addSubview:lineV];
    
    // 右边按钮
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(SCREENW - 40, 25, 32, 32)];
    [btn setImage:[UIImage imageNamed:@"weather"] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(weatherClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
}

-(void)weatherClick
{
    SNKWeatherViewController *vc = [[SNKWeatherViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - Scroll View Delegate Methods
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    _currentIndex = scrollView.contentOffset.x / SCREENW;
    _navTabBar.currentItemIndex = _currentIndex;
    
    /** 当scrollview滚动的时候加载当前视图 */
    UIViewController *viewController = (UIViewController *)_subViewControllers[_currentIndex];
    viewController.view.frame = CGRectMake(_currentIndex * SCREENW, 0, SCREENW, _mainView.kdj_height);
    [_mainView addSubview:viewController.view];
    [self addChildViewController:viewController];
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    _currentIndex = scrollView.contentOffset.x / SCREENW;
    
    // 即将开始滚动的时候,释放播放器
    SNKVideoViewController *viewController = (SNKVideoViewController *)_subViewControllers[_currentIndex];
    [viewController finish];
}

#pragma mark - SNKNavTabBarDelegate
- (void)itemDidSelectedWithIndex:(NSInteger)index withCurrentIndex:(NSInteger)currentIndex {
    // 先释放播放器
    [self scrollViewWillBeginDragging:_mainView];
    
    if (currentIndex - index >= 2 || currentIndex - index <= -2) {
        [_mainView setContentOffset:CGPointMake(index * SCREENW, 0) animated:NO];
    } else {
        [_mainView setContentOffset:CGPointMake(index * SCREENW, 0) animated:YES];
    }
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO];
}

@end
